package com.example.myapplication

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.example.myapplication.databinding.ActivityMainBinding
import androidx.core.net.toUri

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    private lateinit var activityLauncher: ActivityResultLauncher<Intent>

    // 应用入口点
    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 将xml布局文件转换为视图对象
        binding = ActivityMainBinding.inflate(layoutInflater)
        // 将视图绑定生成的根布局
        setContentView(binding.root)

        // 检查是否居右悬浮框权限
//        if (checkOverlayPermission()) {
//            startFloatingService()
//        }

        activityLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode == RESULT_OK) {
                val data: Intent? = result.data
                println(data)
                println("666666666")
                // 处理返回数据‌:ml-citation{ref="3,4" data="citationList"}
            }
        }

        val navView: BottomNavigationView = binding.navView

        // 获取导航控制器
        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment
        val navController = navHostFragment.navController
//         Passing each menu ID as a set of Ids because each
//         menu should be considered as top level destinations.
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home, R.id.navigation_dashboard
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)

        // 检查无障碍服务是否已启用
        if (!isAccessibilityServiceEnabled()) {
//            startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS))
            Log.e("Main","无障碍服务未启用")
        }else{
            Log.d("Main","无障碍服务已开启")
        }
    }

    private fun isAccessibilityServiceEnabled(): Boolean {
        val enabledServices = Settings.Secure.getString(
            contentResolver,
            Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES
        )
        return enabledServices?.contains(packageName) == true
    }

    /**
     * 检查悬浮框权限
     */
//    private fun checkOverlayPermission(): Boolean {
//        if (!Settings.canDrawOverlays(this)) {
//            requestOverlayPermission()
//            return false
//        } else {
//            return true
//        }
//    }

    /**
     * 请求悬浮框权限
     */
    private fun requestOverlayPermission() {
        AlertDialog.Builder(this)
            .setTitle("需要开启悬浮权限")
            .setMessage("请允许显示在其他应用上方")
            .setPositiveButton("去设置") { _, _ ->
                val intent = Intent(
                    Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    "package:$packageName".toUri()
                )
                activityLauncher.launch(intent)
            }.setNegativeButton("取消", null)
            .show()
    }

    /**
     * 启动悬浮框
     */
//    private fun startFloatingService() {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            startForegroundService(Intent(this, HomeService::class.java))
//        } else {
//            startService(Intent(this, HomeService::class.java))
//        }
//    }
}